package com.xb.demo.controller;

import com.xb.demo.entity.Click;
import com.xb.demo.utils.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.URL;
import java.net.URLDecoder;

/**
 * @ClassName ClickController
 * @Description TODO
 * @Author YLZ
 * @Date 2018/7/24 11:16
 * @Version 1.0
 **/
@RestController
@RequestMapping("/log")
public class ClickController {

    String resultJson = null;

    @RequestMapping(value = {"/click"},method = RequestMethod.GET)
    public void click(HttpServletRequest req) throws Exception{

        String lm_id = req.getParameter("lm_id");
        String useragent = req.getHeader("User-Agent");
        String referer = req.getParameter("referer");
        URL URL = new URL(req.getParameter("url"));
        String host = URL.getHost();
        String url = req.getParameter("url");
        String screen = req.getParameter("screen");
        String gpu = req.getParameter("gpu");
        String title = req.getParameter("title");
        String content = URLDecoder.decode(req.getParameter("content"));
        String engine = req.getParameter("engine");
        String isShow = req.getParameter("isShow");
        String site_id = req.getParameter("site_id");
        String browser = BrowserUtils.getBrowser(useragent).brand;
        String ip = IPAddrUtils.getIpAddr(req);
//        String ip= "122.112.13.238";
        String country = IPparseUtils.getArea(ip).get(1).toString();
        String province = IPparseUtils.getArea(ip).get(2).toString();
        String city = IPparseUtils.getArea(ip).get(3).toString();
        String isp = IPparseUtils.getArea(ip).get(5).toString();
        String lon = IPparseUtils.getArea(ip).get(9).toString();
        String lat = IPparseUtils.getArea(ip).get(10).toString();
        String addtime = DateUtils.getSystemTime("yyyy-MM-dd HH:mm:ss", Long.valueOf(System.currentTimeMillis()));
        int times = Integer.parseInt(DateUtils.getString2(System.currentTimeMillis(),"HH"));
        String uuid = req.getParameter("uuid");
        String x = req.getParameter("x");
        String y = req.getParameter("y");

        Click click = new Click();
        click.setLm_id(lm_id);
        click.setUseragent(useragent);
        click.setReferer(referer);
        click.setHost(host);
        click.setUrl(url);
        click.setScreen(screen);
        click.setGpu(gpu);
        click.setTitle(title);
        click.setContent(content);
        click.setEngine(engine);
        click.setIsShow(isShow);
        click.setSite_id(site_id);
        click.setBrowser(browser);
        click.setIp(ip);
        click.setCountry(country);
        click.setProvince(province);
        click.setCity(city);
        click.setIsp(isp);
        click.setLon(lon);
        click.setLat(lat);
        click.setAddtime(addtime);
        click.setTimes(times);
        click.setUuid(uuid);
        click.setX(x);
        click.setY(y);

        resultJson = JsonUtils.obj2JsonString(click);
        KafkaUtils.sendMessage(resultJson, "click_log");

//        System.out.println(resultJson);

//        return resultJson;
    }
}
